﻿/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * Geophysical Computational Tools & Library (GCTL)
 *
 * Copyright (c) 2023  Yi Zhang (yizhang-geo@zju.edu.cn)
 *
 * GCTL is distributed under a dual licensing scheme. You can redistribute 
 * it and/or modify it under the terms of the GNU Lesser General Public 
 * License as published by the Free Software Foundation, either version 2 
 * of the License, or (at your option) any later version. You should have 
 * received a copy of the GNU Lesser General Public License along with this 
 * program. If not, see <http://www.gnu.org/licenses/>.
 * 
 * If the terms and conditions of the LGPL v.2. would prevent you from using 
 * the GCTL, please consider the option to obtain a commercial license for a 
 * fee. These licenses are offered by the GCTL's original author. As a rule, 
 * licenses are provided "as-is", unlimited in time for a one time fee. Please 
 * send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget 
 * to include some description of your company and the realm of its activities. 
 * Also add information on how to contact you by electronic and paper mail.
 ******************************************************/

#include "linear_mesh_3d.h"

void gctl::linear_mesh_3d::init(std::string in_name, std::string in_info, double xmin, double ymin, 
	double zmin, const array<double> &xsizes, const array<double> &ysizes, const array<double> &zsizes)
{
	if (initialized)
	{
		std::string err_str = "The object is already initialized. From void linear_mesh_3d::init(...)";
		throw runtime_error(err_str);
	}

	base_mesh::init(LINEAR_MESH, MESH_3D, in_name, in_info);

	lm_xmin  = xmin;
	lm_ymin  = ymin;
	lm_zmin  = zmin;
	lm_xbnum = xsizes.size();
	lm_ybnum = ysizes.size();
	lm_zbnum = zsizes.size();
	lm_xsizes.resize(lm_xbnum);
	lm_ysizes.resize(lm_ybnum);
	lm_zsizes.resize(lm_zbnum);

	node_num = (lm_xbnum+1) * (lm_ybnum+1) * (lm_zbnum+1);
	ele_num  = lm_xbnum * lm_ybnum * lm_zbnum;

	for (int i = 0; i < lm_xbnum; i++)
	{
		lm_xsizes[i] = xsizes[i];
	}

	for (int i = 0; i < lm_ybnum; i++)
	{
		lm_ysizes[i] = ysizes[i];
	}

	for (int i = 0; i < lm_zbnum; i++)
	{
		lm_zsizes[i] = zsizes[i];
	}

	nodes.resize(node_num);
	elements.resize(ele_num);

	int tmp_index;
	double x_p, y_p, z_p = lm_zmin - 0.5*lm_zsizes[0];
	for (int k = 0; k < lm_zbnum+1; k++)
	{
		y_p = lm_ymin - 0.5*lm_ysizes[0];
		for (int j = 0; j < lm_ybnum+1; j++)
		{
			x_p = lm_xmin - 0.5*lm_xsizes[0];
			for (int i = 0; i < lm_xbnum+1; i++)
			{
				tmp_index = i+j*(lm_xbnum+1)+k*(lm_xbnum+1)*(lm_ybnum+1);
				nodes[tmp_index].id = tmp_index;
				nodes[tmp_index].x  = x_p;
				nodes[tmp_index].y  = y_p;
				nodes[tmp_index].z  = z_p;
				if (i < lm_xbnum) x_p += lm_xsizes[i];
			}
			if (j < lm_ybnum) y_p += lm_ysizes[j];
		}
		if (k < lm_zbnum) z_p += lm_zsizes[k];
	}

	for (int k = 0; k < lm_zbnum; k++)
	{
		for (int j = 0; j < lm_ybnum; j++)
		{
			for (int i = 0; i < lm_xbnum; i++)
			{
				tmp_index = i+j*lm_xbnum+k*lm_xbnum*lm_ybnum;
				elements[tmp_index].id = tmp_index;
				elements[tmp_index].vert[0] = nodes.get(i+j*(lm_xbnum+1)+k*(lm_xbnum+1)*(lm_ybnum+1));
				elements[tmp_index].vert[1] = nodes.get(i+1+j*(lm_xbnum+1)+k*(lm_xbnum+1)*(lm_ybnum+1));
				elements[tmp_index].vert[2] = nodes.get(i+1+(j+1)*(lm_xbnum+1)+k*(lm_xbnum+1)*(lm_ybnum+1));
				elements[tmp_index].vert[3] = nodes.get(i+(j+1)*(lm_xbnum+1)+k*(lm_xbnum+1)*(lm_ybnum+1));
				elements[tmp_index].vert[4] = nodes.get(i+j*(lm_xbnum+1)+(k+1)*(lm_xbnum+1)*(lm_ybnum+1));
				elements[tmp_index].vert[5] = nodes.get(i+1+j*(lm_xbnum+1)+(k+1)*(lm_xbnum+1)*(lm_ybnum+1));
				elements[tmp_index].vert[6] = nodes.get(i+1+(j+1)*(lm_xbnum+1)+(k+1)*(lm_xbnum+1)*(lm_ybnum+1));
				elements[tmp_index].vert[7] = nodes.get(i+(j+1)*(lm_xbnum+1)+(k+1)*(lm_xbnum+1)*(lm_ybnum+1));
				elements[tmp_index].dl = nodes.get(i+j*(lm_xbnum+1)+k*(lm_xbnum+1)*(lm_ybnum+1));
				elements[tmp_index].ur = nodes.get(i+1+(j+1)*(lm_xbnum+1)+(k+1)*(lm_xbnum+1)*(lm_ybnum+1));
			}
		}
	}

	initialized = true;
	return;
}

void gctl::linear_mesh_3d::load_binary(std::string filename)
{
	if (initialized)
	{
		std::string err_str = "The object is already initialized. From void linear_mesh_3d::load_binary(...)";
		throw runtime_error(err_str);
	}

	std::ifstream infile;
	gctl::open_infile(infile, filename, ".2m", 
		std::ios::in|std::ios::binary);

	// 读入网格头信息
	load_headinfo(infile, LINEAR_MESH, MESH_3D);

	// 读入网格信息
	infile.read((char*)&lm_xbnum, sizeof(int));
	infile.read((char*)&lm_ybnum, sizeof(int));
	infile.read((char*)&lm_zbnum, sizeof(int));
	infile.read((char*)&lm_xmin, sizeof(double));
	infile.read((char*)&lm_ymin, sizeof(double));
	infile.read((char*)&lm_zmin, sizeof(double));

	lm_xsizes.resize(lm_xbnum);
	lm_ysizes.resize(lm_ybnum);
	lm_zsizes.resize(lm_zbnum);
	node_num = (lm_xbnum+1) * (lm_ybnum+1) * (lm_zbnum+1);
	ele_num  = lm_xbnum * lm_ybnum * lm_zbnum;

	for (int i = 0; i < lm_xbnum; i++)
	{
		infile.read((char*)lm_xsizes.get(i), sizeof(double));
	}

	for (int i = 0; i < lm_ybnum; i++)
	{
		infile.read((char*)lm_ysizes.get(i), sizeof(double));
	}

	for (int i = 0; i < lm_zbnum; i++)
	{
		infile.read((char*)lm_zsizes.get(i), sizeof(double));
	}

	nodes.resize(node_num);
	elements.resize(ele_num);

	int tmp_index;
	double x_p, y_p, z_p = lm_zmin - 0.5*lm_zsizes[0];
	for (int k = 0; k < lm_zbnum+1; k++)
	{
		y_p = lm_ymin - 0.5*lm_ysizes[0];
		for (int j = 0; j < lm_ybnum+1; j++)
		{
			x_p = lm_xmin - 0.5*lm_xsizes[0];
			for (int i = 0; i < lm_xbnum+1; i++)
			{
				tmp_index = i+j*(lm_xbnum+1)+k*(lm_xbnum+1)*(lm_ybnum+1);
				nodes[tmp_index].id = tmp_index;
				nodes[tmp_index].x  = x_p;
				nodes[tmp_index].y  = y_p;
				nodes[tmp_index].z  = z_p;
				if (i < lm_xbnum) x_p += lm_xsizes[i];
			}
			if (j < lm_ybnum) y_p += lm_ysizes[j];
		}
		if (k < lm_zbnum) z_p += lm_zsizes[k];
	}

	for (int k = 0; k < lm_zbnum; k++)
	{
		for (int j = 0; j < lm_ybnum; j++)
		{
			for (int i = 0; i < lm_xbnum; i++)
			{
				tmp_index = i+j*lm_xbnum+k*lm_xbnum*lm_ybnum;
				elements[tmp_index].id = tmp_index;
				elements[tmp_index].vert[0] = nodes.get(i+j*(lm_xbnum+1)+k*(lm_xbnum+1)*(lm_ybnum+1));
				elements[tmp_index].vert[1] = nodes.get(i+1+j*(lm_xbnum+1)+k*(lm_xbnum+1)*(lm_ybnum+1));
				elements[tmp_index].vert[2] = nodes.get(i+1+(j+1)*(lm_xbnum+1)+k*(lm_xbnum+1)*(lm_ybnum+1));
				elements[tmp_index].vert[3] = nodes.get(i+(j+1)*(lm_xbnum+1)+k*(lm_xbnum+1)*(lm_ybnum+1));
				elements[tmp_index].vert[4] = nodes.get(i+j*(lm_xbnum+1)+(k+1)*(lm_xbnum+1)*(lm_ybnum+1));
				elements[tmp_index].vert[5] = nodes.get(i+1+j*(lm_xbnum+1)+(k+1)*(lm_xbnum+1)*(lm_ybnum+1));
				elements[tmp_index].vert[6] = nodes.get(i+1+(j+1)*(lm_xbnum+1)+(k+1)*(lm_xbnum+1)*(lm_ybnum+1));
				elements[tmp_index].vert[7] = nodes.get(i+(j+1)*(lm_xbnum+1)+(k+1)*(lm_xbnum+1)*(lm_ybnum+1));
				elements[tmp_index].dl = nodes.get(i+j*(lm_xbnum+1)+k*(lm_xbnum+1)*(lm_ybnum+1));
				elements[tmp_index].ur = nodes.get(i+1+(j+1)*(lm_xbnum+1)+(k+1)*(lm_xbnum+1)*(lm_ybnum+1));
			}
		}
	}

	initialized = true;

	// 读入模型数据单元
	load_datablock(infile);

	infile.close();
	return;
}

void gctl::linear_mesh_3d::save_binary(std::string filename)
{
	if (!initialized)
	{
		std::string err_str = "The object is not initialized. From void linear_mesh_3d::save_binary(...)";
		throw runtime_error(err_str);
	}

	std::ofstream outfile;
	gctl::open_outfile(outfile, filename, ".2m", 
		std::ios::out|std::ios::binary);

	// 首先输出网格的头信息
	save_headinfo(outfile);

	// 输出网格信息
	outfile.write((char*)&lm_xbnum, sizeof(int));
	outfile.write((char*)&lm_ybnum, sizeof(int));
	outfile.write((char*)&lm_zbnum, sizeof(int));
	outfile.write((char*)&lm_xmin, sizeof(double));
	outfile.write((char*)&lm_ymin, sizeof(double));
	outfile.write((char*)&lm_zmin, sizeof(double));

	for (int i = 0; i < lm_xbnum; i++)
	{
		outfile.write((char*)lm_xsizes.get(i), sizeof(double));
	}

	for (int i = 0; i < lm_ybnum; i++)
	{
		outfile.write((char*)lm_ysizes.get(i), sizeof(double));
	}

	for (int i = 0; i < lm_zbnum; i++)
	{
		outfile.write((char*)lm_zsizes.get(i), sizeof(double));
	}

	// 输出的模型数据单元
	save_datablock(outfile);

	outfile.close();
	return;
}

void *gctl::linear_mesh_3d::get_node(unsigned int idx)
{
	std::string err_str;
	if (!initialized)
	{
		err_str = "The object is not initialized. From gctl::linear_mesh_3d::get_node(...)";
		throw runtime_error(err_str);
	}

	if (idx >= node_num)
	{
		err_str = "Invalid index. From gctl::linear_mesh_3d::get_node(...)";
		throw runtime_error(err_str);
	}

	return nodes.get(idx);
}

void *gctl::linear_mesh_3d::get_element(unsigned int idx)
{
	std::string err_str;
	if (!initialized)
	{
		err_str = "The object is not initialized. From gctl::linear_mesh_3d::get_element(...)";
		throw runtime_error(err_str);
	}

	if (idx >= ele_num)
	{
		err_str = "Invalid index. From gctl::linear_mesh_3d::get_element(...)";
		throw runtime_error(err_str);
	}

	return elements.get(idx);
}

gctl::linear_mesh_3d::linear_mesh_3d() : base_mesh::base_mesh(){}

gctl::linear_mesh_3d::linear_mesh_3d(std::string in_name, std::string in_info, double xmin, double ymin, 
	double zmin, const gctl::array<double> &xsizes, const gctl::array<double> &ysizes, 
	const gctl::array<double> &zsizes)
{
	init(in_name, in_info, xmin, ymin, zmin, xsizes, ysizes, zsizes);
}

gctl::linear_mesh_3d::~linear_mesh_3d(){}

int gctl::linear_mesh_3d::get_xbnum() const
{
	if (!initialized)
	{
		std::string err_str = "The object is not initialized. From void linear_mesh_3d::get_xbnum(...)";
		throw runtime_error(err_str);
	}

	return lm_xbnum;
}

int gctl::linear_mesh_3d::get_ybnum() const
{
	if (!initialized)
	{
		std::string err_str = "The object is not initialized. From void linear_mesh_3d::get_ybnum(...)";
		throw runtime_error(err_str);
	}

	return lm_ybnum;
}

int gctl::linear_mesh_3d::get_zbnum() const
{
	if (!initialized)
	{
		std::string err_str = "The object is not initialized. From void linear_mesh_3d::get_zbnum(...)";
		throw runtime_error(err_str);
	}

	return lm_zbnum;
}

double gctl::linear_mesh_3d::get_xmin() const
{
	if (!initialized)
	{
		std::string err_str = "The object is not initialized. From void linear_mesh_3d::get_xmin(...)";
		throw runtime_error(err_str);
	}

	return lm_xmin;
}

double gctl::linear_mesh_3d::get_ymin() const
{
	if (!initialized)
	{
		std::string err_str = "The object is not initialized. From void linear_mesh_3d::get_ymin(...)";
		throw runtime_error(err_str);
	}

	return lm_ymin;
}

double gctl::linear_mesh_3d::get_zmin() const
{
	if (!initialized)
	{
		std::string err_str = "The object is not initialized. From void linear_mesh_3d::get_zmin(...)";
		throw runtime_error(err_str);
	}

	return lm_zmin;
}

const gctl::array<double> *gctl::linear_mesh_3d::get_xsizes() const
{
	if (!initialized)
	{
		std::string err_str = "The object is not initialized. From void linear_mesh_3d::get_xsizes(...)";
		throw runtime_error(err_str);
	}

	return &lm_xsizes;
}

const gctl::array<double> *gctl::linear_mesh_3d::get_ysizes() const
{
	if (!initialized)
	{
		std::string err_str = "The object is not initialized. From void linear_mesh_3d::get_ysizes(...)";
		throw runtime_error(err_str);
	}

	return &lm_ysizes;
}

const gctl::array<double> *gctl::linear_mesh_3d::get_zsizes() const
{
	if (!initialized)
	{
		std::string err_str = "The object is not initialized. From void linear_mesh_3d::get_zsizes(...)";
		throw runtime_error(err_str);
	}

	return &lm_zsizes;
}

void gctl::linear_mesh_3d::save_gmsh(std::string filename, index_packed_e packed)
{
	std::ofstream outfile;
	gctl::open_outfile(outfile, filename, ".msh");
	gctl::save2gmsh(outfile, elements, nodes, packed);
	outfile.close();
	return;
}

void gctl::linear_mesh_3d::save_gmsh(std::string filename, mesh_data_type_e d_type, output_type_e out_mode, index_packed_e packed)
{
	base_mesh::save_gmsh(filename, d_type, out_mode, packed);
	return;
}

void gctl::linear_mesh_3d::save_gmsh(std::string filename, std::string datname, output_type_e out_mode, index_packed_e packed)
{
	base_mesh::save_gmsh(filename, datname, out_mode, packed);
	return;
}
